package com.example.bookkeepingelf.business.database.service;

import android.widget.TextView;

import com.example.bookkeepingelf.base.utils.StringUtils;
import com.example.bookkeepingelf.business.enums.JsqOperatorToResourceIdEnum;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

public interface CalculatorService {

    /**
     * 显示结果
     *
     * @param showTextView 文本信息
     * @param clickViewId  操作id
     * @return  null则不需要拼接
     */
    String convertTextResult(TextView showTextView, int clickViewId);

    /**
     * 删除一位
     * @param textView
     * @return
     */
    String removeOne(TextView textView);

    String MINI_DROP_STR = ".";

    /**
     * 计算
     *
     * @param number1      数字1
     * @param number2      数字2
     * @param iEnum 计算类型
     * @return 计算结果
     */
    default BigDecimal calculate(BigDecimal number1, BigDecimal number2, JsqOperatorToResourceIdEnum iEnum) {
        switch (iEnum) {
            case OPERATOR_ADD:
                return number1.add(number2).setScale(2, RoundingMode.HALF_UP);
            case OPERATOR_SUB:
                return number1.subtract(number2).setScale(2, RoundingMode.HALF_UP);
            case OPERATOR_MUL:
                return number1.multiply(number2).setScale(2, RoundingMode.HALF_UP);
            case OPERATOR_DIV:
                return number1.divide(number2, 2, RoundingMode.HALF_UP);
        }
        return null;
    }


    default String getStringValue(TextView textView){
        String text = StringUtils.isBlank(textView.getText().toString()) ? "" : textView.getText().toString();
        return text;
    }


    /**
     *
     * @param beforeStrValue
     * @return
     */
    default int miniDropAfter(String beforeStrValue) {
        // 含有小数点
        int dotIndex = beforeStrValue.lastIndexOf(MINI_DROP_STR);
        if (dotIndex != -1) {
            int charsAfterDot = beforeStrValue.length() - dotIndex - 1;
            // charsAfterDot 即为小数点后面字符的数量
            return charsAfterDot;
        }
        return 0;
    }

    /**
     * 是否包含小数点
     * @param strValue
     * @return
     */
    default boolean isHaveMiniDrop(String strValue) {
        return strValue.contains(MINI_DROP_STR);
    }

    default boolean lastHaveMiniDrop(String beforeStrValue) {
        return beforeStrValue.toCharArray()[beforeStrValue.length() - 1] == '.';
    }


    default boolean lastHaveOperator(String beforeStrValue) {
        for (String operator : JsqOperatorToResourceIdEnum.getMathsOperatorList()) {
            if (beforeStrValue.toCharArray()[beforeStrValue.length() - 1] == operator.toCharArray()[0]) {
                return true;
            }
        }
        return false;
    }
    default boolean firstHaveOperator(String beforeStrValue) {
        for (String operator : JsqOperatorToResourceIdEnum.getMathsOperatorList()) {
            if (beforeStrValue.toCharArray()[0] == operator.toCharArray()[0]) {
                return true;
            }
        }
        return false;
    }

    default String getLastSrt(String beforeStrValue) {
        String lastChar = beforeStrValue.substring(beforeStrValue.length() - 1);
        return lastChar;
    }

    default void cleanResult(StringBuilder stringBuilder){
        stringBuilder.delete(0,stringBuilder.length());
    }
    /**
     * 判断是否含有操作符
     *
     * @param strValue
     * @return
     */
    default boolean isHaveOperator(String strValue) {
        for (String operator : JsqOperatorToResourceIdEnum.getMathsOperatorList()) {
            if (strValue.contains(operator)) {
                return true;
            }
        }
        return false;
    }

    default void baseValidate(TextView showTextView) {
        if (Objects.isNull(showTextView)) {
            throw new RuntimeException("showTextView is null");
        }
    }

    default int operatorNumber(String beforeStrValue) {
        int i = 0;
        for (JsqOperatorToResourceIdEnum value : JsqOperatorToResourceIdEnum.values()) {
            for (char c : beforeStrValue.toCharArray()) {
                if (c == value.getOperator().charAt(0)) {
                    i = i + 1;
                }
            }
        }
        return i;
    }

    default int miniDropNumber(String beforeStrValue) {
        int i = 0;
        for (char c : beforeStrValue.toCharArray()) {
            if (c == '.') {
                i = i + 1;
            }
        }
        return i;
    }

}
